package com.dkd.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.dao.VmTypeDao;
import com.dkd.entity.VmTypeEntity;
import com.dkd.entity.dto.VmPageDto;
import com.dkd.entity.vo.VmTypePageVO;
import com.dkd.entity.vo.VmTypeVo;
import com.dkd.service.VmTypeService;
import com.dkd.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class VmTypeServiceImpl extends ServiceImpl<VmTypeDao, VmTypeEntity> implements VmTypeService {

    @Override
    public VmTypePageVO getpage(VmPageDto pageDto) {
        Integer pageSize = pageDto.getPageSize();//每页长度
        Integer pageIndex = pageDto.getPageIndex();//页码索引
        Page<VmTypeEntity> page = new Page<>(pageIndex, pageSize);
        this.lambdaQuery()
                .like(pageDto.getName() != null, VmTypeEntity::getName, pageDto.getName())
                .page(page);
        Long total = page.getTotal();
        return VmTypePageVO.builder()
                .pageIndex(pageIndex.toString())//页码
                .pageSize(pageSize.toString())//每页大小
                .totalCount(String.valueOf(total))//总条数
                .totalPage(String.valueOf(total / pageSize))//总页数
                .currentPageRecords(page.getRecords())//设置返回列表
                .build();
    }

    @Override
    public VmTypeVo getTypeById(Integer typeId) {
        VmTypeEntity vmTypeEntity = this.getById(typeId);
        VmTypeVo vmTypeVo = new VmTypeVo();
        BeanUtils.copyProperties(vmTypeEntity,vmTypeVo);
        return vmTypeVo;
    }

    public boolean addVmType(VmTypeEntity vmTypeEntity) {
        vmTypeEntity.setInnerCode(UUIDUtils.getUUID());
        return this.save(vmTypeEntity);
    }
}
